package com.banksoft.cps.service.impl;

import com.banksoft.cps.bean.*;
import com.banksoft.cps.bean.common.ServiceException;
import com.banksoft.cps.bean.query.SupplierIntoWarehouseQuery;
import com.banksoft.cps.bean.query.SupplierOrderQuery;
import com.banksoft.cps.bean.query.SupplierOutWarehouseQuery;
import com.banksoft.cps.bean.warpper.SupplierIntoWarehouseWarpper;
import com.banksoft.cps.bean.warpper.SupplierOutWarehouseWarpper;
import com.banksoft.cps.dao.*;
import com.banksoft.cps.service.SupplierIntoWarehouseService;
import com.banksoft.cps.service.SupplierOutWarehouseService;
import com.banksoft.cps.util.StringNumber;
import com.banksoft.cps.util.UUIDFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by rongzhao.zhu on 2018/1/12.
 */
@Service
public class SupplierOutWarehouseServiceImpl implements SupplierOutWarehouseService {
    @Autowired
    SupplierOrderDao supplierOrderDao;
    @Autowired
    CompanyDao companyDao;

    @Autowired
    SupplierOutWarehouseDao supplierOutWarehouseDao;

    @Autowired
    SupplierIntoWarehouseDao supplierIntoWarehouseDao;

    @Autowired
    private TransportCompanyDao transportCompanyDao;

    @Autowired
    MaterialDao materialDao;

    @Override
    public List<SupplierOutWarehouseWarpper> list(SupplierOutWarehouseQuery query) {

        //查找入库记录
        SupplierIntoWarehouseQuery supplierIntoWarehouseQuery = new SupplierIntoWarehouseQuery();
        supplierIntoWarehouseQuery.setPage(query.getPage());
        supplierIntoWarehouseQuery.setSize(query.getSize());
        supplierIntoWarehouseQuery.setCompanyId(query.getCompanyId());
        List<SupplierIntoWarehouse> supplierIntoWarehouseList = supplierIntoWarehouseDao.list(supplierIntoWarehouseQuery);
        List<SupplierIntoWarehouseWarpper>  warehouseWarpperList = new ArrayList<>();
        for(SupplierIntoWarehouse a : supplierIntoWarehouseList) {
            SupplierIntoWarehouseWarpper warehouseWarpper = new SupplierIntoWarehouseWarpper();
            BeanUtils.copyProperties(a, warehouseWarpper);
            SupplierOrder supplierOrder = supplierOrderDao.findById(a.getSupplierOrderId());
            warehouseWarpper.setSupplierOrderName(supplierOrder == null ? null : supplierOrder.getName());

            Company company = companyDao.findById(a.getCompanyId());
            warehouseWarpper.setCompanyName(company == null ? null : company.getName());

            TransportCompany transportCompany = transportCompanyDao.findById(supplierOrder.getTransportCompanyId());
            warehouseWarpper.setTransportCompanyName(transportCompany == null ? null : transportCompany.getName());

            warehouseWarpperList.add(warehouseWarpper);
        }

        List<SupplierOutWarehouseWarpper>  supplierOutWarehouseWarpperArrayList = new ArrayList<>();
        for(SupplierIntoWarehouseWarpper supplierIntoWarehouseWarpper : warehouseWarpperList) {
            query.setSupplierOrderId(supplierIntoWarehouseWarpper.getSupplierOrderId());
            List<SupplierOutWarehouse> supplierOutWarehouseList = supplierOutWarehouseDao.list(query);

            for(int i=0; i<supplierOutWarehouseList.size(); i++) {
                SupplierOutWarehouse a = supplierOutWarehouseList.get(i);

                SupplierOutWarehouseWarpper supplierOutWarehouseWarpper = new SupplierOutWarehouseWarpper();
                BeanUtils.copyProperties(a, supplierOutWarehouseWarpper);
                SupplierOrder supplierOrder = supplierOrderDao.findById(a.getSupplierOrderId());
                if(supplierOrder != null) {
                    supplierOutWarehouseWarpper.setSupplierOrderName(supplierOrder.getName());
                    Material material = materialDao.findById(supplierOrder.getMaterialId());
                    if(material != null) {
                        supplierOutWarehouseWarpper.setMaterialName(material.getName());
                    }
                }
                if(i == 0) {
                    supplierOutWarehouseWarpper.setSupplierIntoWarehouseWarpper(supplierIntoWarehouseWarpper);
                }
                supplierOutWarehouseWarpperArrayList.add(supplierOutWarehouseWarpper);
            }
        }
        return supplierOutWarehouseWarpperArrayList;
    }

    @Override
    public long count(SupplierOutWarehouseQuery query) {
        return supplierOutWarehouseDao.count(query);
    }

    @Override
    public void save(SupplierOutWarehouse bean) throws ServiceException {
        if(StringUtils.isNotBlank(bean.getId())){
            supplierOutWarehouseDao.update(bean);
        }else {
            bean.setId(UUIDFactory.random());
            bean.setCreateTime(System.currentTimeMillis());

            //获取入库单
            SupplierOutWarehouseQuery supplierOutWarehouseQuery = new SupplierOutWarehouseQuery();
            supplierOutWarehouseQuery.setSupplierOrderId(bean.getSupplierOrderId());
            List<SupplierOutWarehouse> supplierOutWarehouseList = supplierOutWarehouseDao.list(supplierOutWarehouseQuery);
            if(supplierOutWarehouseList == null || supplierOutWarehouseList.size() == 0) {
                SupplierIntoWarehouseQuery supplierIntoWarehouseQuery = new SupplierIntoWarehouseQuery();
                supplierIntoWarehouseQuery.setSupplierOrderId(bean.getSupplierOrderId());
                List<SupplierIntoWarehouse> supplierIntoWarehouseList = supplierIntoWarehouseDao.list(supplierIntoWarehouseQuery);
                if(supplierIntoWarehouseList != null && supplierIntoWarehouseList.size() != 0) {
                    SupplierIntoWarehouse supplierIntoWarehouse = supplierIntoWarehouseList.get(0);
                    if(!StringUtils.isNotEmpty(supplierIntoWarehouse.getAmount())) {
                        SupplierOrder supplierOrder = supplierOrderDao.findById(bean.getSupplierOrderId());
                        bean.setKc(StringNumber.sub(supplierOrder.getAmount(), bean.getAmount()));
                    } else {
                        bean.setKc(StringNumber.sub(supplierIntoWarehouse.getAmount(), bean.getAmount()));
                    }
                } else {
                    SupplierOrder supplierOrder = supplierOrderDao.findById(bean.getSupplierOrderId());
                    bean.setKc(StringNumber.sub(supplierOrder.getAmount(), bean.getAmount()));
                }
            } else {
                SupplierOutWarehouse supplierOutWarehouse = supplierOutWarehouseList.get(0);
                bean.setKc(StringNumber.sub(supplierOutWarehouse.getKc(), bean.getAmount()));
            }
            supplierOutWarehouseDao.insert(bean);
        }
    }

    @Override
    public SupplierOutWarehouse findById(String id) {
        return supplierOutWarehouseDao.findById(id);
    }
}
